package bai.publicwelfare_backend.controller.user;

import bai.publicwelfare_backend.common.utils.JwtUtil;
import bai.publicwelfare_backend.common.utils.Result;
import bai.publicwelfare_backend.pojo.dto.user.ChangeUserDTO;
import bai.publicwelfare_backend.pojo.dto.user.LoginDTO;
import bai.publicwelfare_backend.pojo.dto.user.RegisterDTO;
import bai.publicwelfare_backend.pojo.entity.user.User;
import bai.publicwelfare_backend.pojo.vo.user.AuthVO;
import bai.publicwelfare_backend.service.impl.user.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/auth")
public class UserController {
    private static final Logger log = LogManager.getLogger(JwtUtil.class); // 修复日志记录器

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result<AuthVO> register(@RequestBody @Valid RegisterDTO registerDTO) {
        AuthVO authVO = userService.register(registerDTO);
        return Result.success(authVO, "注册成功");
    }

    @PostMapping("/login")
    public Result<AuthVO> login(@Valid @RequestBody LoginDTO dto) {
        AuthVO authVO = userService.login(dto);
        return Result.success(authVO, "登录成功");
    }
    @GetMapping("/userinfo")
    public Result<User> getUserInfo(HttpServletRequest request) {
        // 1. 获取当前用户及Token
        String token = request.getHeader("Authorization");
        User user = userService.getUserInfo(token);
        return Result.success(user, "获取用户信息成功");
    }
    @PostMapping("/setUserName")
    public Result<String> setUserName( @RequestHeader("Authorization") String authHeader, @Valid @RequestBody ChangeUserDTO changeUser) {
        System.out.println("changeUser:" + changeUser);
         userService.setUserName(authHeader, changeUser);
        return Result.success("修改用户名成功");
    }
    @PostMapping("/logout")
    public void logout(HttpServletRequest request) {
        // 1. 获取当前用户及Token
        String token = request.getHeader("Authorization");
        userService.logout(token);
        return ;
    }


}
